package com.smzd.projforge.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.core.dao.AbstractDao;
import com.smzd.core.mappers.IMapper;
import com.smzd.core.utils.db.RPage;
import com.smzd.core.utils.db.SRPage;
import com.smzd.projforge.domain.DynaAttribute;
import com.smzd.projforge.domain.DynaClass;
import com.smzd.projforge.domain.Project;
import com.smzd.projforge.mappers.DynaAttributeMapper;
import com.smzd.projforge.mappers.DynaClassMapper;

@Component
public class DynaClassDao extends AbstractDao<DynaClass> {
	@Autowired
	private DynaClassMapper mapper;
	@Autowired
	private DynaAttributeMapper attrMapper;
	@Override
	protected IMapper<DynaClass> getMapper() {
		return mapper;
	}

	public DynaClass getDynaClassWithAttributes(int id){
		DynaClass clazz = this.getElemById(id);
		SRPage<DynaClass, DynaAttribute> paging = new SRPage<>();
		paging.setScope(clazz);
		paging.setPageSize(999);
		List<DynaAttribute> attributes = attrMapper.getElemsByPage(paging) ;
		for(DynaAttribute attr : attributes){
			attr.setScope(clazz);
		}
		clazz.setDynaAttributes(attributes);
		return clazz;
	}


	public RPage<DynaClass> getMasterClassListByPage(RPage<DynaClass> page) {
		preparePage(page);
		List<DynaClass> elems = mapper.getMasterClassListByPage(page);
		page.setElems(elems);
		int total = mapper.getElemsCount(page);
		page.setTotalCount(total);
		completePage(page);
		return page;
	}

	public SRPage<Project, DynaClass> getMTMRelationClassCandidates(SRPage<Project, DynaClass> page) {
		preparePage(page);
		List<DynaClass> elems = mapper.getMTMRelationClassCandidates(page);
		page.setElems(elems);
		page.setTotalCount(elems.size());
		completePage(page);
		return page;
	}

    public List<DynaClass> getClassesByProject(Project project) {
        SRPage<Project, DynaClass> classPage = new SRPage<>();
        classPage.setScope(project);
        classPage.setPageSize(999);
        List<DynaClass> classes = mapper.getElemsByPage(classPage);
        return classes;
    }
}
